package org.hackystat.iw.projectoverviewer.sensordata;

import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.datatype.XMLGregorianCalendar;
import org.hackystat.iw.projectoverviewer.test.ProjectOverviewerTestHelper;
import org.hackystat.sensorbase.client.SensorBaseClient;
import org.hackystat.sensorbase.client.SensorBaseClientException;
import org.hackystat.sensorbase.resource.projects.jaxb.Project;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Test;

/**
 * A test for the sensor data collector.
 * 
 * @author Jianfei Liao
 */
public class TestSensorDataCollector extends ProjectOverviewerTestHelper {
  /**
   * Test the sensor data collection.
   * 
   * @throws FileNotFoundException If errors occur. 
   */
  @Test
  public void testDataCollection() throws FileNotFoundException { 
    // connect to the sensor base server using the sensor data collector
    String host = this.getSensorBaseHostName();
    String email = "admin@hackystat.org";
    String password = "admin@hackystat.org";
    
    SensorDataCollector collector = new SensorDataCollector(host, email, password);
    SensorBaseClient client = collector.getSensorBaseClient();
    
    try {
      // put up a testing project with test data
      org.hackystat.sensorbase.resource.projects.jaxb.Project testProject = new Project();
      String projectName = "TestProject";
      testProject.setName(projectName);
      testProject.setOwner(email);
      XMLGregorianCalendar startTime = Tstamp.makeTimestamp();
      testProject.setStartTime(startTime);
      testProject.setEndTime(Tstamp.incrementDays(startTime, 10));
      client.putProject(testProject);
      
      // delete all old sensor data
      client.deleteSensorData(email);
      
      String svnUrl = "http://informative-workspace.googlecode.com/svn/trunk/";
      collector.getRepository(svnUrl, projectName);
      
      XMLGregorianCalendar endTime = Tstamp.makeTimestamp();
      XMLGregorianCalendar dataTime = Tstamp.incrementMinutes(endTime, -30);
      startTime = Tstamp.incrementHours(endTime, -1);
            
      // create sclc sensor data
      Map<String, String> sclcMap = new HashMap<String, String>();
      sclcMap.put("Resource", "TestSensorDataCollector.java");
      sclcMap.put("Timestamp", dataTime.toString());
      sclcMap.put("Tool", "SCLC");
      sclcMap.put("SensorDataType", "FileMetric");
      sclcMap.put("SourceLines", "100");
      SensorData sclcData = client.makeSensorData(sclcMap); 
      assertNotNull("SCLC data is not null", sclcData);
      
      // create dev event data
      Map<String, String> devEventMap = new HashMap<String, String>();
      devEventMap.put("Resource", "TestSensorDataCollector.java");
      devEventMap.put("Timestamp", dataTime.toString());
      devEventMap.put("Tool", "Eclipse");
      devEventMap.put("SensorDataType", "DevEvent");
      devEventMap.put("Subtype", "Save");
      SensorData devEventData = client.makeSensorData(devEventMap);
      assertNotNull("DevEvent data is not null", devEventData);
      
      // create coverage data
      Map<String, String> coverageMap = new HashMap<String, String>();
      coverageMap.put("Resource", "TestSensorDataCollector.java");
      coverageMap.put("Timestamp", dataTime.toString());
      coverageMap.put("Tool", "Emma");
      coverageMap.put("SensorDataType", "Coverage");
      coverageMap.put("ClassName", "SensorDataCollector");
      coverageMap.put("method_Covered", "6");
      coverageMap.put("method_Uncovered", "4");
      SensorData coverageData = client.makeSensorData(coverageMap);
      assertNotNull("Coverage data is not null", coverageData);
      
      // put the sensor data to the sensor base client
      client.putSensorData(sclcData);
      collector.getSensorData(projectName, "FileMetric", startTime, endTime);
      
      client.putSensorData(coverageData);
      collector.getSensorData(projectName, "Coverage", startTime, endTime);
      
      client.putSensorData(devEventData);
      collector.getSensorData(projectName, "DevEvent", startTime, endTime);
    }
    catch (SensorBaseClientException e) {
      e.printStackTrace();
    }

    // test the writing functions
    try {
      // remove the test project
      String userHome = System.getProperty("user.home");
      File tm3File = new File(userHome + "/.hackystat/projectoverviewer/TestProject.tm3");
      File xmlFile = new File(userHome + "/.hackystat/projectoverviewer/TestProject.xml");
      collector.write(tm3File, xmlFile);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
